package org.whut.practice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.whut.practice.pojo.Inspection;
import org.whut.practice.service.InspectionService;
import org.whut.practice.utils.Response;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InspectionController {

    @Autowired
    InspectionService inspectionService;

    @ResponseBody
    @PostMapping("/inspection")
    public Map<String, Object> addInspection(@RequestBody Inspection inspection) {
        int count = inspectionService.addInspection(inspection);
        return count > 0 ? Response.makeOkResponse(count, "Successfully add inspection data") :
                Response.makeErrorResponse("Failed to add inspection data");
    }


    @ResponseBody
    @GetMapping("/inspection")
    public Map<String, Object> getInspectionList(Inspection inspection) {
        List<Inspection> list = inspectionService.selectInspections(inspection.getTime(), inspection.getClasses(),
                inspection.getCategory(), inspection.getName(), inspection.getGroup());
        return list != null ? Response.makeOkResponse(list, "Successfully get inspection data") :
                Response.makeErrorResponse("Failed to get inspection data");
    }

}